﻿using System.Windows;
using System.Windows.Controls;

namespace MediaBrowser.CustomWindow
{
    /// <summary>
    /// Contrôle personnalisé ImageButton utilisé dans la title bar de ma fenêtre personnalisé
    /// </summary>
    internal class ImageButton : Button
    {
        private double OriginalWidth;
        private double OriginalHeight;

        static ImageButton()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ImageButton), new FrameworkPropertyMetadata(typeof(ImageButton)));
        }

        public ImageButton()
        {
            OriginalWidth = Width;
            OriginalHeight = Height;
        }

        #region properties

        public string ImageOver
        {
            get { return (string)GetValue(ImageOverProperty); }
            set { SetValue(ImageOverProperty, value); }
        }

        public string ImageNormal
        {
            get { return (string)GetValue(ImageNormalProperty); }
            set { SetValue(ImageNormalProperty, value); }
        }

        public string ImageDown
        {
            get { return (string)GetValue(ImageDownProperty); }
            set { SetValue(ImageDownProperty, value); }
        }

        public bool Hidden
        {
            get { return (bool)GetValue(HiddenProperty); }
            set { SetValue(HiddenProperty, value); }
        }


        #endregion

        #region dependency properties

        public static readonly DependencyProperty ImageNormalProperty =
           DependencyProperty.Register(
               "ImageNormal", typeof(string), typeof(ImageButton));


        public static readonly DependencyProperty ImageOverProperty =
          DependencyProperty.Register(
              "ImageOver", typeof(string), typeof(ImageButton));

        public static readonly DependencyProperty ImageDownProperty =
        DependencyProperty.Register(
            "ImageDown", typeof(string), typeof(ImageButton));

        public static readonly DependencyProperty HiddenProperty =
        DependencyProperty.Register(
            "Hidden", typeof(bool), typeof(ImageButton),
            new UIPropertyMetadata(false, new PropertyChangedCallback(HiddenContentChanged)));

        #endregion

        private static void HiddenContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            ImageButton img = d as ImageButton;

            if (img.Hidden)
            {
                img.Width = 0;
                img.Height = 0;
                int indexColumn = Grid.GetColumn(img);
                ((Grid)img.Parent).ColumnDefinitions[indexColumn].Width = new GridLength(0);
            }
            else
            {
                img.Width = img.OriginalWidth;
                img.Height = img.OriginalHeight;
                int indexColumn = Grid.GetColumn(img);
                ((Grid)img.Parent).ColumnDefinitions[indexColumn].Width = new GridLength(25);
            }
        }

    }
}
